const state = {
  vncTabs:[],
  activeTab:null,
}

const mutations = {
  ADD_TAB: (state, tab) => {
    if (state.vncTabs.some(v => v.mid === tab.mid)) return
    state.vncTabs.push(tab)
    state.activeTab = tab.mid
  },
  DEL_TAB: (state, mid) => {
    for (const [i, v] of state.vncTabs.entries()) {
      if (v.mid === mid) {
        state.vncTabs.splice(i, 1)
        break
      }
    }
    if(mid = state.activeTab) state.activeTab = null
  },
  ACTIVE_TAB: (state, mid) => {
    state.activeTab = mid
  },
  CLEAR_TABS: (state,) => {
    state.vncTabs=[]
  },
}

const actions = {
  addTab({ commit }, data) {
    commit('ADD_TAB', data)
  },
  delTab({ commit }, data) {
    commit('DEL_TAB', data)
  },
  activeTab({ commit }, data) {
    commit('ACTIVE_TAB', data)
  },
  clearTabs({ commit }) {
    commit('CLEAR_TABS')
  },
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
